------------------------------------------------------------------
--  Rock5's BuyFromItemShop userfunction
--               Version 1.3.1
--
--  Syntax:
--  BuyFromItemShop(itemGUID, secondaryPassword [, number] )
--  BuyPresentFromItemShop(itemGUID, secondaryPassword, recipient [,subject] [,body])
--
--  Arguments:
--    itemGUID (type number) - The uniquie id of the shop selection. See below for instructions on how to get this value.
--    secondaryPassword (type string) - Your secondary password for that account
--    number (type number) - The number you want to buy of that selection. Optional. If not specified will, buy only one.
--    recipient (type string) - The person you want to send the present to.
--    subject (type string) - The subject of the message sent with the present. Optional.
--    body (type string) - The body of the message sent with the present. Optional.
--
--  Examples:
--    To buy 2 items with GUID of 100
--       BuyFromItemShop(100, "secpass', 2)
--    To buy an item with GUID of 100 and send it as a present
--       BuyPresentFromItemShop(100, "secpass', "recipientName")
--
--  WARNING! GUIDs are unique per server. GUIDs given by other users might be wrong for your
--           server. So it's important to get your own GUIDs using the instructions below.
--
--  How to get 'itemGUID':
--    1. Open the Item Shop.
--    2. Find the item you want to buy. Make sure it's the right item. I take no responsability if your make a mistake.
--    3. Select the 'Buy' button so that the 'Buy' popup appears.
--    4. Now enter this command,
--         /script SendSystemChat(ItemMallFrame.selectItem.GUID)
--    5. The number it prints is the 'itemGUID' of the item you want to buy.

local function commonBuy(_itemGUID, _secondaryPassword, _numberOrRecipient, _subject, _body)
	RoMScript("} ItemMallFrame:Show(); ItemMallFrame:Hide(); a={")
	if type(_itemGUID) ~=  "number" then
		print("You must use the Item Shop itemGUID number when using the '"..func.."' function.")
		return
	end

	if _secondaryPassword == nil then
		print("You must specify a secondary password for the '"..func.."' function to work.")
		return
	end

	yrest(1000)
	local staticpopup = RoMScript("} CheckPasswordState(); a=StaticPopup_Visible('PASSWORD_CONFIRM'); if a then _G[a..'EditBox']:ClearFocus(); end a={a")
	if staticpopup then
		RoMScript(staticpopup.."EditBox:SetText(\"".._secondaryPassword.."\")") rest(1000)
		RoMScript("StaticPopup_EnterPressed("..staticpopup..");") yrest(1000)
		RoMScript(staticpopup..":Hide();");
		yrest(1000)
		local passwordfailed = RoMScript("StaticPopup_Visible('PASSWORD_FAILED')")
		if passwordfailed then
			error("Rent(): Wrong secondary password.")
		end
	end

	RoMScript("} ItemMallFrame.lock=1 a={") yrest(500)
	if type(_numberOrRecipient) == "number" then
		RoMScript("CIMF_ShoppingBuy(".._itemGUID..",".._numberOrRecipient..")"); yrest(500) -- buy items
	else
		if _subject == nil then _subject = "Gift from "..player.Name end
		if _body == nil then _body = "" end
		RoMScript("CIMF_MailGift(".._itemGUID..", \"".._numberOrRecipient.."\", \"".._subject.."\", \"".._body.."\")"); yrest(500) -- buy items
	end

end

local func = ""
function BuyFromItemShop(_itemGUID, _secondaryPassword, _number )
	if _number == nil then
		_number = 1
	else
		_number = tonumber(_number)
		if type(_number) ~= "number" then
			print("Invalid number to buy used with the 'BuyFromItemShop' function.")
			return
		end
	end

	func = "BuyFromItemShop"
	commonBuy(_itemGUID, _secondaryPassword, _number )
end

function BuyPresentFromItemShop(_itemGUID, _secondaryPassword, _recipient, _subject, _body)
	if type(_recipient) ~= "string" then
		print("Invalid recipient used with the 'BuyPresentFromItemShop' function. Type 'string' expected.")
		return
	end

	func = "BuyPresentFromItemShop"
	commonBuy(_itemGUID, _secondaryPassword, _recipient, _subject, _body )
end

